package cn.poka.device;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.util.Log;
import android_serialport_api.SerialPort;
import cn.poka.util.DeviceInfo;

public class FingerDevice {
	
	private static FingerDevice fingerDevice;
	
	private static SerialPort fingerSerialPort;
	
	private static com.BRMicro.SerialPort mSerialPort;
	
	/** ͨ��RFID���л��ӿڣ��õ����������� */
	private static InputStream mInput;
	
	/** ͨ��RFID���л��ӿڣ��õ���������� */
	private static OutputStream mOutput;
	
	//private static GPIO powerIO;
	
	private FingerDevice(){
		
	}
	
	public SerialPort getFingerSerialPort(){
		return fingerSerialPort;
	}
	
	public static FingerDevice getInstance(){
		if(fingerDevice == null){
			if(fingerSerialPort == null){
				//powerIO = new GPIO(GlobalConstant.IO_FINGER_POWER);
				mSerialPort = new com.BRMicro.SerialPort();
				
				try {
					Thread.sleep(400);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				File rfidFile = new File(DeviceInfo.FINGER_FILE);
				try {
					fingerSerialPort = new SerialPort(rfidFile, 
							DeviceInfo.FINGER_KEY, 0);
					
					mInput = fingerSerialPort.getInputStream();
					mOutput = fingerSerialPort.getOutputStream();
					
					fingerDevice = new FingerDevice();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}
		
		return fingerDevice;
	}
	
	public void enable(){
		mSerialPort.psam_poweron();
		mSerialPort.setGpioHigh(74);
		mSerialPort.switch2Channel(2);
		
//		if(powerIO != null){
//			powerIO.setGPIO(GlobalConstant.ENABLE_IO);
//			powerIO.setGPIO(71);
//			Log.d("tag__", "finger enable--");
//		}
	}
	
	public void disenable(){
		if(mSerialPort != null){
			mSerialPort.psam_poweroff();
		}
		
//		if(powerIO != null){
//			powerIO.setGPIO(GlobalConstant.DISABLE_IO);
//			powerIO.setGPIO(70);
//		}
	}
	
	/**
	 * �ر�RFID�豸
	 */
	public void close(){
		if(fingerSerialPort != null){
			fingerSerialPort.close();
			fingerSerialPort = null;
			try {
				mInput.close();
				mOutput.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		disenable();
		
		if(fingerDevice != null){
			fingerDevice = null;
		}
	}
	
	/**
	 * ����ָ��
	 * @param cmd
	 */
	public void sendCMD(byte[] cmd){
		try {
			mOutput.write(cmd);
			mOutput.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * ��ȡ��Ӧ֡
	 * @return
	 */
	public byte[] read(){
		byte[] responseData = null;
		byte[] response = null;
		int available = 0 ;
		int index = 0;
		int headIndex = 0;
		//500ms����ѯ�������������ݷ���
		try {
		while(index < 10){
			Thread.sleep(50);
			available = mInput.available();
			//������������
			if(available > 7) break;
			index++;
		}
		if(available > 0){
			responseData = new byte[available];
			mInput.read(responseData);
			for(int i = 0; i < available; i++){
				//if(responseData[i] == R500_HEAD){
//					headIndex = i;
//					break;
//				}
			}
			response = new byte[available - headIndex];
			System.arraycopy(responseData, headIndex, response, 0, response.length);
			
		}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		return response;
	}
	
	public void enroll(){
		Log.d("tag__", "FingerActivity----����ָ��ʶ���豸��ʼ��ָ��-11--begin");
		String luru = "ENROLL_FINGER";
		byte[] t = luru.getBytes();
		byte[] temp = new byte[76];
		for (int i = 0; i < t.length; ++i){
			temp[i] = t[i];
		}
		//int result = FingerDevice.getInstance().getFingerSerialPort().serialWrite(temp);
		if(fingerSerialPort != null){
			int result = fingerSerialPort.serialWrite(temp);
			Log.d("tag__", "FingerActivity----����ָ��ʶ���豸��ʼ��ָ��-11--end  result=" + result);
		}
	}
	
	public void quit(){
		Log.d("tag__", "----�˳�--quit-------");
		String close = "QUIT";
		byte[] tt = close.getBytes();
		byte[] tempclose = new byte[76];
		for (int i = 0; i < tt.length; ++i){
			tempclose[i] = tt[i];
		}
		//FingerDevice.getInstance().getFingerSerialPort().serialWrite(tempclose);
		if(fingerSerialPort != null){
			int result = fingerSerialPort.serialWrite(tempclose);
			Log.d("tag__", "----�˳�--quit-------" + result);
		}
	}
	
	/**
	 * ����ָ��
	 * @param temp
	 * @return
	 */
	public int serialWrite(byte[] temp){
		return fingerSerialPort.serialWrite(temp);
	}
	
	/**
	 * �Ƿ�ѹ
	 * @return
	 */
	public int isFingerPressed(){
		return fingerSerialPort.isFingerPressed();
	}
	
	/**
	 * ��ȡָ��ͼ��
	 * @param dataType
	 * @return
	 */
	public byte[] fingerRead(byte dataType){
		return fingerSerialPort.fingerRead(dataType);
	}
	
	/**
	 * ��ȡָ������ֵ
	 * @return
	 */
	public byte[] fingerReadIval(){
		return fingerSerialPort.fingerReadIval();
	}
	
}


















